package match_data

import (
	"fmt"
	"github.com/spf13/cast"
	"longmen/server/config/global"
	"longmen/server/internal/rpc/tencentim/config"
	"longmen/server/internal/services/nami/services/sports/basketball/bbcache"
	"longmen/server/internal/services/nami/services/sports/football/fbcache"
)

func GetMatchHistoryData(roomId string) (historyMsg []string) {

	//嘗試獲取比分
	if scoreMsg, has := scoreHistory(roomId); has {
		historyMsg = append(historyMsg, scoreMsg)
		global.Log.Debug("after GetMatchHistoryData history : ", historyMsg)
	}

	//嘗試獲取技術統計
	if statsMsg, has := statsHistory(roomId); has {
		historyMsg = append(historyMsg, statsMsg)
	}

	////嘗試獲取籃球細節
	//if recordMsg, has := teamRecordHistory(roomId); has {
	//	historyMsg = append(historyMsg, recordMsg)
	//}

	//嘗試獲取競猜
	//if quizMsg, has := quizHistory(roomId); has {
	//	historyMsg = append(historyMsg, quizMsg)
	//}
	//global.Log.Debug("return GetMatchHistoryData history: ", historyMsg)
	return historyMsg
}

func statsHistory(roomId string) (string, bool) {
	matchId := cast.ToInt(roomId)
	var cache string

	cache = bbcache.NewStatsCache().GetStatsCache(matchId)
	if len(cache) > 0 {
		return cache, true
	}
	cache = fbcache.NewStatsCache().GetStatsCache(matchId)
	if len(cache) > 0 {
		return cache, true
	}
	return "", false
}

func quizHistory(roomId string) (string, bool) {
	quizKey := fmt.Sprint(config.LiveStreamConst, ":", config.QuizConst, ":", roomId)
	quizMsg, err := global.GetRds().Get(quizKey).Result()
	if err != nil {
		global.Log.Debugf("查無競猜資訊, room_id: %v ", roomId)
	} else {
		return quizMsg, true
	}
	return "", false
}
